
using System;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using CUtil;
using CStock;
using System.Diagnostics;

namespace CapitalReSources
{
	/// <summary>
	/// Summary description for ListViewIntraday.
	/// </summary>
	public class ListViewIntraday : ListViewWithEnum ,IListViewIntraday, IContextProvider
	{
		private IMainClass mainClass;
		private IProgramContext programContext;

		private System.Windows.Forms.ColumnHeader columnHeaderName;
		private System.Windows.Forms.ColumnHeader columnHeaderShareID;
		private System.Windows.Forms.ColumnHeader columnHeaderTimeGet;
		private System.Windows.Forms.ColumnHeader columnHeaderTimeTransaction;
		private System.Windows.Forms.ColumnHeader columnHeaderQuote;
		private System.Windows.Forms.ColumnHeader columnHeaderBid;
		private System.Windows.Forms.ColumnHeader columnHeaderAsk;
		private System.Windows.Forms.ColumnHeader columnHeaderVolume;
		private System.Windows.Forms.ColumnHeader columnHeaderVolumeTotal;
		private System.Windows.Forms.ColumnHeader columnHeaderBidVolume;
		private System.Windows.Forms.ColumnHeader columnHeaderAskVolume;

		/// <summary>
		/// 
		/// </summary>
		public ListViewIntraday()
		{
			this.View = System.Windows.Forms.View.Details;
            //old relict from .NET 1.1
            ListViewIntraday.CheckForIllegalCrossThreadCalls = false;
			//------------------------
            InitializeComponent();

			try
			{
				this.mainClass = MainClass.Instance;
				this.mainClass.ListViewIntraday = this;
			}
			catch
			{
				//this occurs in design mode - do nothing
			}
			
		}
		/// <summary>
		/// dataholder - the main source of data
		/// </summary>
		public IProgramContext ProgramContext
		{
			get
			{
				return this.programContext;
			}
			set
			{
				programContext = value;
			
			}
		}
	
		private void InitializeComponent()
		{
			this.columnHeaderName = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderShareID = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderTimeGet = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderTimeTransaction = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderQuote = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderBid = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderAsk = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderVolume = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderVolumeTotal = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderBidVolume = new System.Windows.Forms.ColumnHeader();
			this.columnHeaderAskVolume = new System.Windows.Forms.ColumnHeader();


			this.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
																							   this.columnHeaderName,
																							   this.columnHeaderShareID,
																							   this.columnHeaderTimeGet,
																							   this.columnHeaderTimeTransaction,
																							   this.columnHeaderQuote,
																							   this.columnHeaderBid,
																							   this.columnHeaderAsk,
																							   this.columnHeaderVolume,
																							   this.columnHeaderVolumeTotal,
																							   this.columnHeaderBidVolume,
																							   this.columnHeaderAskVolume});
		
			// 
			// columnHeaderName
			// 
			this.columnHeaderName.Text = "Name";
			// 
			// columnHeaderShareID
			// 
			this.columnHeaderShareID.Text = "ShareID";
			// 
			// columnHeaderTimeGet
			// 
			this.columnHeaderTimeGet.Text = "TimeGet";
			// 
			// columnHeaderTimeTransaction
			// 
			this.columnHeaderTimeTransaction.Text = "TimeTransaction";
			// 
			// columnHeaderQuote
			// 
			this.columnHeaderQuote.Text = "Quote";
			// 
			// columnHeaderBid
			// 
			this.columnHeaderBid.Text = "Bid";
			// 
			// columnHeaderAsk
			// 
			this.columnHeaderAsk.Text = "Ask";
			this.columnHeaderAsk.Width = 98;
			// 
			// columnHeaderVolume
			// 
			this.columnHeaderVolume.Text = "Volume";
			// 
			// columnHeaderVolumeTotal
			// 
			this.columnHeaderVolumeTotal.Text = "Total Volume";
			// 
			// columnHeaderBidVolume
			// 
			this.columnHeaderBidVolume.Text = "BidVolume";
			// 
			// columnHeaderAskVolume
			// 
			this.columnHeaderAskVolume.Text = "AskVolume";

		}
		public void UpdateQuotes()
		{
			this.Items.Clear();
			Thread myThread =new Thread(new ThreadStart(this.UpdateWebQuotesThread) );
			myThread.Start();
			
			

			
			//QuotesFromWebWindow qfw = new QuotesFromWebWindow();			
			//qfw.ShowDialog();
		}
	
		
		public void UpdateWebQuotesThread()
		{
			for(int i = 0;i < this.programContext.TradeEconomy.Portfolio_Selected.DataItems.Count;i++)
			{
                IDataItem MyData = (IDataItem)(this.programContext.TradeEconomy.Portfolio_Selected.DataItems)[i];
                IQuoteWeb qw = YahooFeedSockets.GetQuotes(MyData.DataClass);
				WriteItem(MyData.DataClass.Name,MyData.DataClass.ID,qw);


			}
		}
		private void WriteItem(string name,string WKN, IQuoteWeb qw)
		{
			string[] arr=new String[this.Columns.Count+1];
			try
			{
				arr[0]=name;
				arr[1]=WKN;

				arr[2]=qw.DATE.ToShortTimeString();
				arr[3]=qw.TimeLastTransaction.ToShortTimeString();
				arr[4]=qw.LastPrice .ToString();
				arr[5]=qw.Ask.ToString();
				arr[6]=qw.Bid.ToString();
				//arr[7]=qw.Volume.ToString();
				arr[8]=qw.VolumeTotal.ToString();
				arr[9]=qw.BidSize .ToString();
				arr[10]=qw.AskSize.ToString();
			}
			catch
			{
				//do nothing
			}
			ListViewItem MyListItem = new ListViewItem(arr);
			this.Items.Add(MyListItem);
			this.Refresh();
			
		}
        //private void AttachToTWSChanged()
        //{
			
        //    try
        //    {
        //        CQuoteUpdateIntraday.TWSControl tWSControl = CQuoteUpdateIntraday.TWSControl.Instance;
        //        tWSControl.TickEvent -= new TickEvent(this.OnTick);
        //        tWSControl.TickEvent += new TickEvent(this.OnTick);

        //        tWSControl.TickCompleteEvent -= new TickEvent(this.OnTickComplete);
        //        tWSControl.TickCompleteEvent += new TickEvent(this.OnTickComplete);
        //    }
        //    catch(Exception exc)
        //    {
        //        Debug.Assert(false,"Error connecting to TradeWorkStation", exc.Message);
        //    }
			

			

        //            //(programContext.Portfolios).Changed += new ChangedEventHandler(this.OnPortfoliosChanged);
			
			
			
        //}
		public void OnTick(object sender, TickEventArgs e)
		{
//			//string str = sender.Name;
//			string message = e.PropertyName;
//			switch (message)
//			{
//				case ("Complete"):
//				{
//					break;
//				}
//			}
//			Contract c = e.NewValue as Contract;
			//IPortfolio port = sender as IPortfolio;
		}
		public void OnTickComplete(object sender, TickEventArgs e)
		{
//			//string str = sender.Name;
//			string message = e.PropertyName;
//			switch (message)
//			{
//				case ("Complete"):
//				{
//					break;
//				}
//			}
//			Contract c = e.NewValue as Contract;
			//IPortfolio port = sender as IPortfolio;
		}
        //public void IBQuotes()
        //{
        //    try
        //    {
        //        CQuoteUpdateIntraday.TWSControl tWSControl = CQuoteUpdateIntraday.TWSControl.Instance;
        //        AttachToTWSChanged();
			
			

		

        //        for(int i = 0; i < this.programContext.TradeEconomy.Portfolios[0].DataItems.Count; i++)
        //        {
        //            IDataItem MyDI = this.programContext.TradeEconomy.Portfolios[0].DataItems[i];

        //            IDataClass MyDC = this.programContext.TradeEconomy.Portfolios[0].DataItems[i].DataClass;
        //            IContract contr = new Contract();
        //            contr.QuoteWeb.Symbol = MyDI.Symbol;
				

        //            switch (MyDI.Exchange)
        //            {
        //                case "XETRA":
        //                {
        //                    contr.Exchange = "IBIS";
        //                    break;
        //                }
        //                case "EUREX":
        //                {
        //                    contr.Exchange = "DTB";
        //                    break;
        //                }
        //            }
        //            switch (MyDI.MarketType)
        //            {
        //                case "SHARE":
        //                {
        //                    contr.SecType = "STK";
        //                    break;
        //                }
        //                case "FUTURE":
        //                {
        //                    contr.SecType = "FUT";
        //                    break;
        //                }
        //                case "INDEX":
        //                {
        //                    contr.SecType = "IND";
        //                    break;
        //                }
        //            }
        //            if(contr.QuoteWeb.Symbol != string.Empty)
        //            {
        //                tWSControl.RequestMarketData(contr);
        //                return;
        //            }
				
        //            //IQuoteWeb qw = CQuoteUpdateIntraday.Access = new CQuoteUpdateIntraday.Access();
        //            //WebQuotes.GetQuotes(MyData.DataClass);
        //            //WriteItem(MyData.DataClass.Name,MyData.DataClass.ID,qw);
        //        }

        //    }
        //    catch(Exception exc)
        //    {
        //        Debug.Assert(false,"Error connecting to TradeWorkStation", exc.Message);
        //    }

        //    //QuotesFromWebWindow qfw = new QuotesFromWebWindow();			
        //    //qfw.ShowDialog();
        //}

	}
}
